<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>解构函数</title>
  <script>
    /**
     * 1. 解构函数-数组解构
     */
    function fun1(){
      let arr = [1,2,3,4,5]
      const [a,b,c,d,e] = arr
      console.log("a = ",a)
      console.log("b = ",b)
      console.log("c = ",c)
      console.log("d = ",d)
      console.log("e = ",e)
    }
    // fun1()
    /**
     * 2. 解构函数-对象解构 （非常重要）
     */
    function fun2(){
      // 2.1 定义一个对象
      let obj = {
        "sid":1001,
        "sname":"张三",
        "age":20,
        "addr":"上海",
        sayHello(){
          console.log(obj.sname + "，你好！")
        }
      }
      /**
       * 2.2 解构对象
       * 原理：
       *  ① 解构时：号前的代表原对象的属性名，：号后的代表新的变量名
       *  ② 解构时：如果原对象的属性名与变量名一致，可以省略变量名
        */
      const {sid,sname,age,addr,sayHello} = obj
      console.log("sid = ",sid)
      console.log("sname = ",sname)
      console.log("age = ",age)
      console.log("addr = ",addr)
      sayHello()
    }
    fun2()
  </script>
</head>
<body>

</body>
</html>
